﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Web.SessionState;

namespace AjaxLibrary
{
    class buyer_category : IHttpHandler,IRequiresSessionState
    {
        bool IHttpHandler.IsReusable
        {
            get { return true; }
        }

        #region IHttpHandler 成员


        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string nodeId = context.Request["node"];

            if (!string.IsNullOrEmpty(nodeId))
            {
                Coolite.Ext.Web.TreeNodeCollection nodes = new Coolite.Ext.Web.TreeNodeCollection(false);

                DataTable dt = new DataTable();

                if (nodeId == "0")
                {
                    dt = logic.category.listWithCompanyRange(logic.customer.getCompanyId());
                }
                else
                {
                    dt = logic.category.getByParent(Convert.ToInt32(nodeId));
                }


                foreach (DataRow dr in dt.Rows)
                {
                    Coolite.Ext.Web.TreeNode node = new Coolite.Ext.Web.TreeNode();
                    node.Text = dr["name"].ToString();
                    node.NodeID = dr["categoryId"].ToString();
                    if (dr["treelevel"].ToString() == "1")
                    {
                        node.Leaf = true;
                        node.Href = "javascript:getList(" + dr["categoryId"].ToString() + ");";
                    }
                    nodes.Add(node);
                }
                context.Response.Write(nodes.ToJson());
                context.Response.End();
            }
        }
        #endregion
    }
}
